package com.biz.primus.ms.order.promotion;

import com.biz.primus.model.order.vo.request.OrderRequestVO;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.ms.order.promotion.helper.OrderPromotionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 促销管理
 *
 * @author Tiger Feng
 * @date 2017年11月09日
 */
@Component
public class PromotionHandlerManager {
    private static PromotionHandlerManager promotionHandlerManager;
    @Autowired
    private OrderPromotionHelper orderPromotionHelper;

    @PostConstruct
    public void init() {
        promotionHandlerManager = this;
        promotionHandlerManager.orderPromotionHelper = this.orderPromotionHelper;
    }

    /**
     * 促销结果计算
     *
     * @param orderPromotionDetailVo
     * @param orderRequestVO
     */
    public static void execute(OrderPromotionDetailVo orderPromotionDetailVo, OrderRequestVO orderRequestVO) {
        if (orderPromotionDetailVo == null) {
            return;
        }
        if (orderRequestVO.getPromotion() == null) {
            return;
        }
        // 传入的促销详情 和 用户选择的促销不匹配
        if (orderPromotionDetailVo.getId().longValue() != orderRequestVO.getPromotion().getId()) {
            return;
        }
        AbstractPromotionHandler handler = null;
        // *SECKILL("秒杀", "秒"),
        // *CUT_PRICE_SALE("降价拍", "拍"),～～～～～～～
        // *DISCOUNT("折扣促销", "折"),～～～～～～
        // *PRESELL("预售", "预"),
        // *PURCHASE_GIFT("买赠", "赠"),～～～～～～～
        // *PURCHASE_LOTTERY("购买抽奖", "奖"),
        // *RAISE_PRICE_REDEMPTION("加价换购", "换"),
        // *SIGNATURE("签到/评价有礼", "礼"), ～～～～～～～x
        // * SPECIAL_OFFER("简单特价", "特"),
        // *USER_FIRST_ORDER_CUT("首单立减", "减"),～～～～～～～x
        // * LUCKY_ORDER("下单有礼", "礼");
        switch (orderPromotionDetailVo.getPromotionType()) {
            case DISCOUNT:
                // 折扣促销
                handler = new DiscountPromotionHandler(
                        orderPromotionDetailVo,
                        orderRequestVO,
                        promotionHandlerManager.orderPromotionHelper
                );
                break;
            case CUT_PRICE_SALE:
                // 降价拍
                handler = new DepreciatePromotionHandler(
                        orderPromotionDetailVo,
                        orderRequestVO,
                        promotionHandlerManager.orderPromotionHelper
                );
                break;
            case PURCHASE_GIFT:
                // 买赠
                handler = new PurchaseGiftPromotionHandler(
                        orderPromotionDetailVo,
                        orderRequestVO,
                        promotionHandlerManager.orderPromotionHelper
                );
                break;
            case SIGNATURE:
                // 签到/评价有礼
                handler = new SignaturePromotionHandler(
                        orderPromotionDetailVo,
                        orderRequestVO,
                        promotionHandlerManager.orderPromotionHelper
                );
                // 暂时不做
                handler = null;
                break;
            case USER_FIRST_ORDER_CUT:
                //首单立减
                handler = new FirstOrderCutPromotionHandler(
                        orderPromotionDetailVo,
                        orderRequestVO,
                        promotionHandlerManager.orderPromotionHelper
                );
                break;
            default:
                break;
        }
        if (handler != null) {
            handler.execute();
        }
    }
}
